package com.coze.lg.cozeagent.controller.oldtest;

import com.coze.lg.cozeagent.config.CozeConfig;
import com.coze.lg.cozeagent.controller.AipSpeech;
import com.coze.openapi.service.service.CozeAPI;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class socketSocket extends Thread {
    private final int port;
    private final CozeAPI cozeAPI;
    // 初始化一个AipSpeech
    private final AipSpeech client;

    public socketSocket(int port) {
        this.port = port;
        this.cozeAPI = new CozeConfig().cozeAPI();
        this.client = new AipSpeech();
    }

    @Override
    public void run() {
        try {
            // 创建ServerSocket对象，指定监听的端口号
            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("等待客户端连接...");

            while (true) {
                // 监听客户端的连接请求
                Socket clientSocket = serverSocket.accept();
                ((Runnable) () -> {
                    SessionSocket sessionSocket = new SessionSocket(clientSocket, client, cozeAPI);
                    sessionSocket.do_start();
                }).run();
                System.out.println("客户端已连接");
            }
        } catch (IOException e) {
            System.err.println("Socket错误: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("未预期的错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
